﻿using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using Iesi.Collections.Generic;
using Kotonoha.Objects;

namespace Kotonoha.Server.Objects.Model {
    /// <summary>
    /// Represents a japanese word with reading and translations
    /// </summary>
    public class Word
    {
        public Word() {
            id = -1;
            translations = new List<string>();
            examples = new List<Example>();
        }

        private long id;

        [XmlAttribute]
        public virtual long Id {
            get { return id; }
            set { id = value; }
        }

        [XmlAttribute]
        public virtual string Writing { get; set; }

        [XmlAttribute]
        public virtual string Reading { get; set; }

        private IList<string> translations;
        public virtual IList<string> Translations {
            get { return translations; }
            set { translations = value; }
        }

        private IList<Example> examples;
        public virtual IList<Example> Examples {
            get { return examples; }
            set { examples = value; }
        }


        [XmlAttribute]
        public virtual DateTime AddedDate { get; set; }

        public virtual TimedVariable Repetition { get; set; }

        private IList<Tag> tags = new List<Tag>();

        [XmlIgnore]
        public virtual IList<Tag> Tags {
            get { return tags; }
            set { tags = value; }
        }

        private IDictionary<string, string> additional = new Dictionary<string, string>();
        [XmlElement(ElementName = "Additional")]
        public virtual IDictionary<string, string> Additional {
            get { return additional; }
            set { additional = value; }
        }

        public virtual int getAdditionalInt(string name, int defVal) {
            string s;
            if (!additional.TryGetValue(name, out s)) {
                return defVal;
            }
            return int.Parse(s);
        }

        public virtual bool Equals(Word obj) {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            return obj.Id == Id;
        }

        public override bool Equals(object obj) {
            if (ReferenceEquals(null, obj)) {
                return false;
            }
            if (ReferenceEquals(this, obj)) {
                return true;
            }
            if (obj.GetType() != typeof (Word)) {
                return false;
            }
            return Equals((Word) obj);
        }

        public override int GetHashCode() {
            return Id.GetHashCode();
        }
    }

    /// <summary>
    /// Represents example sentence
    /// </summary>
    public class Example {
        public string Content { get; set; }
        public string Translation { get; set; }
    }
}